import sys
import os
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)

        toolBar =QToolBar()
        cb = QCheckBox('Separator')
        cb.stateChanged.connect(self.OnSeparatorChanged)
        toolBar.addWidget(cb)

        cb = QCheckBox('Wrapping')
        cb.stateChanged.connect(self.OnWrappingChanged)
        toolBar.addWidget(cb)

        toolBar.addWidget(QLabel('Step:'))
        self.stepEdit = QLineEdit()
        self.stepEdit.editingFinished.connect(self.SetStep)
        self.stepEdit.setInputMask('d0')
        toolBar.addWidget(self.stepEdit)

        toolBar.addWidget(QLabel('Prefix:'))
        self.prefixEdit = QLineEdit()
        self.prefixEdit.editingFinished.connect(self.SetPrefix)        
        toolBar.addWidget(self.prefixEdit)

        toolBar.addWidget(QLabel('Suffix:'))
        self.suffixEdit = QLineEdit()
        self.suffixEdit.editingFinished.connect(self.SetSuffix)        
        toolBar.addWidget(self.suffixEdit)

        toolBar.addWidget(QLabel('StepType:'))
        stepType = QComboBox()
        stepType.addItems(QSpinBox.StepType.__members__.keys())
        stepType.currentTextChanged.connect(self.StepTypeChanged)
        toolBar.addWidget(stepType)

        toolBar.addWidget(QLabel('Min:'))
        self.minEdit = QLineEdit()
        #self.minEdit.setInputMask('###')
        toolBar.addWidget(self.minEdit)

        toolBar.addWidget(QLabel('Max:'))
        self.maxEdit = QLineEdit()
        #self.maxEdit.setInputMask('###')
        toolBar.addWidget(self.maxEdit)

        btn =QPushButton('SetRange')
        btn.clicked.connect(self.SetRange)
        toolBar.addWidget(btn)

        btn =QPushButton('ShowValue')
        btn.clicked.connect(self.ShowValue)
        toolBar.addWidget(btn)       

        layout.addWidget(toolBar)
        self.spinBox=QSpinBox()
        self.spinBox.setRange(0,10000)
        self.spinBox.setValue(1000)
        layout.addWidget(self.spinBox)
        self.setLayout(layout)
    def OnSeparatorChanged(self,state):
        self.spinBox.setGroupSeparatorShown(state)
        self.parent.OnShowMessage(f"Set GroupSeparatorShown={state}")
    def OnWrappingChanged(self,state):
        self.spinBox.setWrapping(state)
        self.parent.OnShowMessage(f"Set Wrapping={state}")
    def SetStep(self):
        step = int(self.stepEdit.text())
        self.spinBox.setSingleStep(step)
        self.parent.OnShowMessage(f'Set SingleStep: {step}')
    def SetPrefix(self):
        prefix= self.prefixEdit.text()
        self.spinBox.setPrefix(prefix)
        self.parent.OnShowMessage(f'Set Prefix: {prefix}')
    def SetSuffix(self):
        suffix= self.suffixEdit.text()
        self.spinBox.setSuffix(suffix)
        self.parent.OnShowMessage(f'Set Suffix: {suffix}')
    def StepTypeChanged(self,text):
        type = QSpinBox.StepType[text]
        self.spinBox.setStepType(type)
        self.parent.OnShowMessage(f'Set StepType: {type}')
    def SetRange(self):
        min =int(self.minEdit.text())
        max =int(self.maxEdit.text())
        self.spinBox.setRange(min,max)
        self.parent.OnShowMessage(f'Set Range: {min}~{max}')
    def ShowValue(self):
        self.parent.OnShowMessage(f'SpinBox value:{self.spinBox.value()}')
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet